<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>教师请假申请-云校通-教师</title>
<jsp:include page="/WEB-INF/page/common/_header.jsp"></jsp:include>
<script src="<%=basePath %>/js/picker.js"></script>
<script src="<%=basePath %>/js/select.js"></script>
<body ontouchstart id="tjjsqj">
	<div class="weui_tab tab-bottom">
		<div class="weui-header bg-blue"> 
	  		<div class="weui-header-left"> <a onclick="back()" href="javascript:" class="icon icon-109 f-white">取消</a> </div>
	    	<h1 class="weui-header-title">教师请假申请</h1>
	    </div>
		<div class="weui_tab_bd">
	        <div class="weui_cells"style="margin: 0;">
				<div class="weui_cell">
					<div class="weui_cell_hd">
						<label class="weui_label"><i class="icon icon-85 f20 f-blue"></i> 请假人</label>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<input class="weui_input tr" value="${user.teacher.name }" readonly/>
					</div>
				</div>
				<div class="weui_cell weui_cell_select weui_select_after">
	                <div class="weui_cell_hd">
	                    <label class="weui_label"><i class="icon icon-67 f20 f-blue"></i> 请假类型</label>
	                </div>
	                <div class="weui_cell_bd weui_cell_primary weui_cell_ft">
	                	如：病假，事假等
	                </div>
	                <input id="leaveType" type="text" class="picker-input" value="" readonly/>
	            </div>
				<div class="weui_cell weui_cell_select">
					<div class="weui_cell_hd">
						<label class="weui_label"><i class="icon icon-51 f20 f-blue"></i> 开始时间</label>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<input id="startDate" class="weui_input weui_input-select" placeholder="请选择" readonly="">
					</div>
				</div>
				<div class="weui_cell weui_cell_select">
					<div class="weui_cell_hd">
						<label class="weui_label"><i class="icon icon-51 f20 f-blue"></i> 结束时间</label>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<input id="endDate" class="weui_input weui_input-select" placeholder="请选择" readonly>
					</div>
				</div>
				<div class="weui_cell weui_cell_select weui_select_after">
	                <div class="weui_cell_hd">
	                    <label class="weui_label"><i class="icon icon-67 f20 f-blue"></i> 请假时长</label>
	                </div>
	                <div class="weui_cell_bd weui_cell_primary weui_cell_ft">
	                	请选择
	                </div>
	                <input id="leaveHours" type="text" class="picker-input" value="" readonly/>
	            </div>
				<div class="weui_cell weui_cell_select weui_select_after">
	                <div class="weui_cell_hd">
	                    <label class="weui_label"><i class="icon icon-67 f20 f-blue"></i> 请假事由</label>
	                </div>
	            </div>
	            <div class="weui_cell">
	                <div class="weui_cell_bd weui_cell_primary">
	                    <textarea id="leaveReason" class="weui_textarea" placeholder="请输入请假事由" rows="3"></textarea>
	                </div>
	            </div>
			</div>
	        
	        <div class="weui_cells weui_cells_access mt5">
				<div class="weui_cell">
					<div class="weui_cell_hd" style="margin-right: 5px;">
						<i class="icon icon-54 f18 f-blue"></i>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
	                    <p> 图片</p>
	                </div>
					<div id="filePicker" class="weui_btn weui_btn_mini weui_btn_primary f-white bg-blue">上传</div>
				</div>
				<div class="weui_cell moments__post">
					<div class="weui_cell_bd">
						<div class="thumbnails weui_uploader_files">
							<ul id="img1" class="img-click">
							
							</ul>
			           </div>
			       </div>
			   </div>
			</div>
		   <div class="weui_cells weui_cells_access mt5">
				<div class="weui_cell" id="check">
					<div class="weui_cell_bd absolut">
	                    <p><i class="icon icon-99 f18 f-blue"></i> 审批人</p>
	                </div>
				</div>
				<div class="gzsp_wrap">
					<ul class="checkBtn"></ul>
					<div style="margin-left: 30px;" id="addBtn"><span class="add checkBtn"></span></div>
				</div>
		   </div>
	       <div class="weui_btn_area" id="leave-addBtn">
		        <a href="javascript:" class="weui_btn bg-blue">提交申请</a>
		    </div>
		    <div style="margin-bottom: 55px;"></div>
		</div>
	</div>
	
	<!--弹窗-->				
	<div class="weui_mask ueureiu weui_mask_visible" style="display: none;z-index:600">
	</div>
	<!--End弹窗-->
	
	<!-- 老师组织架构 start-->
	<div id="ls_div_box" class="weui_tab tab-bottom" style="display:none;z-index: 600;background-color: #fff;">
		<div class="weui_tab_bd">
	        <div class="weui-header bg-blue"> 
	        	<div class="weui-header-left" id="teacherBack"> <a class="icon icon-109 f-white">取消</a>  </div>
				<h1 class="weui-header-title">石门附小组织架构</h1>
			</div>
	        
	        <!--下拉选择-->
	        <div class="page-bd">  
		         <ul id="teacherList">
		     	 </ul>
		    </div>
	    	<script id="teacher-table-template" type="text/x-handlebars-template">	
					<li class="list1 js-show">
		                <div class="page-category js-categoryInner">
							<div class="weui_cells" style="margin: 0;">
								<ul class="list_3">
								{{#each list}}
						            <li><div class="weui_cell_hd list_c3" style="display: inline-flex;padding-left: 10px;">
											<label class="weui_cells_checkbox weui_check_label" style="padding-right: 15px;" for="{{groupId}}_{{teacherId}}">
												<input type="checkbox" class="weui_check" value="{{teacherId}}" id="{{groupId}}_{{teacherId}}">
					        					<i class="weui_icon_checked"></i>
											</label>
											<label class="tea_name">{{teacherName}}</label>
										</div>
									</li>
								{{/each}}
								</ul>
							</div>
		                </div>
		            </li>
			</script>
	    	<!--End下拉选择-->
		</div>
	</div>
</body>
<jsp:include page="/WEB-INF/page/common/_footer.jsp"></jsp:include>
<script type="text/javascript">
var apploverData = {};
$(function(){
   var nowDate = getTime(new Date(), 'yyyy-MM-dd');
   $("#startDate").datetimePicker({title:"选择开始时间",min:nowDate,max:'2050-10-01'});
   $("#endDate").datetimePicker({title:"选择结束时间",min:nowDate,max:'2050-10-01'});
   $("#leaveType").picker({
     title: "请选择请假类型",
     toolbarCloseText:'确定',
     onClose:function(){
     	$(this.input).closest(".weui_cell").find(".weui_cell_ft").text(this.cols[0].displayValue);
     },
     cols: [
       {
         textAlign: 'center',
         values: [1,2,3,4,5,6,7,8,9,10],
         displayValues:['事假','病假','年假','调休','婚假','产假','陪产假','丧假','带薪假','周日晚上和周五下午'],
       }
     ]
   });
   $("#leaveHours").picker({
     title: "请选择请假时长",
     toolbarCloseText:'确定',
     onClose:function(){
     	$(this.input).closest(".weui_cell").find(".weui_cell_ft").text(this.cols[0].displayValue);
     },
     cols: [
       {
         textAlign: 'center',
         values: ['1节课','2节课','0.5天','1天','1.5天','2天','2.5天','3天','3天以上']
       }
     ]
   });
   initApprover();
   $("#leave-addBtn").click(function(){
	   leaveSubmit($(this));
   })
   //老师组织架构返回
   $("#teacherBack").click(function() {
		$('.ueureiu').hide();
		$("#ls_div_box").hide();
   });
   $("#leaveHours").change(function() {
	   getApprover();
   })
   $("#leaveType").change(function() {
	   getApprover();
   })
   //老师组织架构弹出
   $(".checkBtn").click(function() {
	 	//上下级拉动
		$("#teacherList .js-category").each(function(i, v) {
	   		v.removeEventListener("click", categoryClick, false);
	   		v.addEventListener('click', categoryClick);
	   	})
	   	var leaveType = $("#leaveType").val();
	 	if(leaveType.length == 0) {
	 		$.alert("请先选择请假类型","");
	 		return;
	 	}
	 	var leaveHours = $("#leaveHours").val();
	 	if(leaveHours.length == 0) {
	 		$.alert("请先选择请假时长","");
	 		return;
	 	}
	 	$('.ueureiu').show();
	 	$("#ls_div_box").show();
		$(".list_c3 input").change(function() {
			var obj = $(this);
			obj.closest("li").find("input")[0].checked = false;
			obj[0].checked=true;
			var name = obj.closest(".list_c3").find(".tea_name").html();
			$(".gzsp_wrap>ul").html("");
			$(".gzsp_wrap>ul").html("<li><div class='gzsp'><span>"+name.substr(name.length-2,name.length)+"</span><i data-id='"+obj.val()+"'>"+name+"</i></div></li>");
			$("#teacherBack").click();
			$("#addBtn").attr("style","display: none;");
		});
	})
	//拍照或从手机相册中选图接口
	$('#filePicker').on('click', function () {
	    wx.chooseImage({
	        count: 6,
	        needResult: 1,
	        sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
	        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
	        success: function (res) {
	       		var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
	       		wxuploadImage(localIds);
	        },
	        fail: function (res) {
	            alterShowMessage("操作提示", JSON.stringify(res), "1", "确定", "", "", "");
	        }

	    });
	});
})
function leaveSubmit(thiz) {
	var leaveType = $("#leaveType").val();
	var startDate = $("#startDate").val();
	var endDate = $("#endDate").val();
	var leaveHours = $("#leaveHours").val();
	var leaveReason = $("#leaveReason").val();
	
	if(leaveType.length == 0) {
		$.alert("请选择请假类型", "");
		return;
	}
	if(startDate.length == 0) {
		$.alert("请选择开始时间","");
		return;
	}
	if(endDate.length == 0) {
		$.alert("请选择结束时间","");
		return;
	}
	if(leaveHours.length ==0) {
		$.alert("请选择请假时长","");
		return;
	}
	if(leaveReason.trim().length == 0) {
		alert("请输入请假原因");
		return;
	}
	var aObj = $($(".gzsp_wrap").find(".gzsp i"));
	var approverId = emptyToNull(aObj.attr("data-id"));
	if(approverId == null) {
		$.alert("请选择审批人", "");
		return;
	}
	var attachIds = new Array();
	$("#img1 .thumbnail").each(function(i, v) {
		attachIds.push($(v).attr("data-id"));
	});
	if(thiz.hasClass("active")) return;
	thiz.addClass("active");
	$.ajax({
   		url : basePath + "/ls/teacher/leave/create",
		type : "post",
		dataType : "json",
		data : {
			leaveType: leaveType,
			startDate: startDate,
			endDate: endDate,
			leaveHours: leaveHours,
			leaveReason: leaveReason,
			ids: attachIds.toString(),
			approver: aObj.html(),
			approverId: approverId
		},
	    success: function(data){
	    	if(data.code == '000') {
	    		$.toast("请假提交成功！");
	    		var url = basePath + "/ls/teacher/leave?v=" + new Date().getTime();
	    		setTimeout("javascript:location.href='"+url+"'", 1000);
	    	}else {
	    		thiz.removeClass("active");
	    		$.toptips(data.msg);
	    	}
	    },
	    error: function(xhr, type){
	      
	    }
	 });
}
function initApprover() {
	$.ajax({
		url: basePath + '/ls/teacher/leave/approver',
		type: 'post',
		dataType: 'json',
		success: function(data) {
			apploverData = data.data;
		}
	});
}
function getApprover(leaveHours) {
	var list = new Array();
	var leaveType = $("#leaveType").val();
	var leaveHours = $("#leaveHours").val();
	if(leaveType == '10') {
		for(var i in apploverData) {
			var subList = apploverData[i].subList;
			if(subList != null && subList.length > 0) {
				for(var j in subList) {
					if(subList[j].ids != null) {
						if(subList[j].ids.indexOf("2001") != -1) {
							list.push(subList[j]);
						}
					}
				}
			}
		}
	}else {
		if(!leaveHours) return;
		if(leaveHours == '1节课' || leaveHours == '2节课') {
			for(var i in apploverData) {
				if(apploverData[i].groupId == 20) {
					list = apploverData[i].subList;
					break;
				}
			}
		}else {
			for(var i in apploverData) {
				var subList = apploverData[i].subList;
				if(subList != null && subList.length > 0) {
					for(var j in subList) {
						if(subList[j].ids != null) {
							if(leaveHours == '0.5天') {
								if(apploverData[i].groupId == 10 && (subList[j].ids.indexOf("1001") == -1 && subList[j].ids.indexOf("2001") == -1)) {
									list.push(subList[j]);
								}
							}
							else if(leaveHours == '3天' || leaveHours == '3天以上') {
								if(subList[j].ids.indexOf("1001") != -1) {
									list.push(subList[j]);
								}
							}else {
								if(subList[j].ids.indexOf("2001") != -1) {
									list.push(subList[j]);
								}
							}
						}
					}
				}
			}
		}
	}
	if(list.length == 1) {
		$("#addBtn").attr("style","display: none;");
		var name = list[0].teacherName;
		$(".gzsp_wrap>ul").html("<li><div class='gzsp'><span>"+name.substr(name.length-2,name.length)+"</span><i data-id='"+list[0].teacherId+"'>"+name+"</i></div></li>");
	}else {
		$(".gzsp_wrap>ul").html("");
		$("#addBtn").attr("style","margin-left: 30px; display: block;");
		var teacherData = {list: list};
		loadTeacherHtml(teacherData);
	}
}
function loadTeacherHtml(teacherData) {
	var myTemplate = Handlebars.compile($("#teacher-table-template").html());
		Handlebars.registerHelper("equal", function(v1,v2,options) {
   		if(v1 == v2) {
   			//满足添加继续执行
   			return options.fn(this);
   		}else {
   			//不满足条件执行{{else}}部分
   			return options.inverse(this);
   		}
   	});
	$('#teacherList').html(myTemplate(teacherData));
}
function categoryClick() {
	$parent = $(this).closest('li');
	if($parent.hasClass('js-show')){
        $parent.removeClass('js-show');
        $parent.find('i').removeClass('icon-35').addClass('icon-74');
    }else{
        $parent.siblings().removeClass('js-show');
        $parent.addClass('js-show');
        $parent.siblings().find('i').removeClass('icon-35').addClass('icon-74');
        $parent.find('i').removeClass('icon-74').addClass('icon-35');
    }
}
function wxuploadImage(localIds) {  
	var localId = localIds.pop();
    wx.uploadImage({  
        localId: localId, // 需要上传的图片的本地ID，由chooseImage接口获得  
        isShowProgressTips: 1, // 默认为1，显示进度提示  
        success: function (res) {  
            mediaId = res.serverId; // 返回图片的服务器端ID  
            wechatMediaDownload(mediaId);
            if(localIds.length > 0){
            	wxuploadImage(localIds);
            }
        },  
        fail: function (error) {  
            alert(Json.stringify(error));  
        }  
    });
}  
function wechatMediaDownload(mediaId) {
   	var params = {};
   	params.mediaId = mediaId;
    params.attachmentType = "image";
    params.relationType = "teacherleave";
    $.ajax({
 		url : basePath + "/ls/attchment/upload/wx",
 		type : "post",
 		data : params,
 		dataType : "json",
 		async : false,
 		success : function(data) {
 			if(data.code != '000') {
 				$.toast(data.msg, "cancel");
 			}else {
 				$('#img1').append('<li class="weui_uploader_file thumbnail" data-id="'+data.data.id+'" style="margin-right:0;margin-bottom:16px;position: relative;"><div class="img-del" onclick="imgDel(this)"><span class="icon icon-26"></span></div><img onclick="imgClick(this)" src="'+ imgUrl + data.data.attachmentUrl +'"/></li>'); 
 			}
 		}, error:function(){
 			$.alert("上传异常","");
  		}
 	});
}
</script>
</html>